package com.xinsoft.controller.basic;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 简单模式
 * 消息生产者
 */
@Controller
@RequestMapping(value = "simple")
public class SimpleProducer {

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    /**
     * 向rabbitMq发送消息
     */
    @RequestMapping(value = "sendMsg")
    @ResponseBody
    public String send(@RequestParam(name = "messageId") String messageId, @RequestParam(name = "messageData") String messageData) {
        SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = simpleDateFormat.format(new Date());
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息发送到队列my_queue中
        rabbitTemplate.convertAndSend("my_queue", map);
        //receive将接收到消息
        System.out.println("rabbitMQ 简单模式消息发送成功！");
        return "true";
    }

}
